/*
 * @Descripttion: 
 * @version: 
 * @Author: yuxintao
 * @Date: 2022-05-24 17:01:09
 * @LastEditors: yuxintao
 * @LastEditTime: 2022-05-24 21:35:32
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

#define SOE_BANDIP 0X128
#define SOE_BANDPORT 0X129
#define SOE_BANDPING 0X12A

typedef struct nf_bandport{
    unsigned short protocol;//2个字节
    unsigned short port;//2个字节
}nf_bandport;

typedef struct band_status{
    unsigned int band_ip;//4个字节 禁止的目的ip地址
    nf_bandport band_port;//禁止的端口(TCP OR UDP)
    unsigned char band_ping;//1个字节 禁止ping回显的标志
}band_status;

int main() {
    band_status status;
    bzero(&status, sizeof(band_status));
    socklen_t len = sizeof(band_status);
    int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
    if (sockfd == -1) {
        perror("socket");
        exit(1);
    }
    char ch;

    //forbid ip
    printf("do you want to forbid ip?[y/n]");
    ch = getchar(); getchar();
    status.band_ip = 0;
    if (ch == 'y') {
        char ipbuf[30];
        printf("please input the dst ip that you want to forbid:");
        scanf("%s", ipbuf);getchar();
        status.band_ip = inet_addr(ipbuf);
    }
    

    //forbid port
    printf("do you want to forbit port?[y/n]");
    ch = getchar(); getchar();
    status.band_port.protocol = 0;
    status.band_port.port = 0;
    if (ch == 'y') {
        printf("TCP OR UDP:[T/U]");
        ch = getchar(); getchar();
        if (ch == 'T') {
            printf("TCP\n");
            status.band_port.protocol = IPPROTO_TCP;
        } else {
            printf("UDP\n");
            status.band_port.protocol = IPPROTO_UDP;
        }
        printf("please input the port:");
        int port;
        scanf("%d", &port); getchar();
        printf("port = %d\n", port);
        status.band_port.port = htons(port);
    }
    


    printf("do you want to forbid ping:[y/n]");
    ch = getchar(); getchar();
    status.band_ping = 0;
    if (ch == 'y')
        status.band_ping = 1;


    
    if (setsockopt(sockfd, IPPROTO_IP, SOE_BANDIP, (void*)&status, len)) {
        perror("setsockopt SOE_BANDIP");
        exit(1);
    }
    if (setsockopt(sockfd, IPPROTO_IP, SOE_BANDPORT, (void*)&status, len)) {
        perror("setsockopt SOE_BANDPORT");
        exit(1);
    }
    if (setsockopt(sockfd, IPPROTO_IP, SOE_BANDPING, (void*)&status, len)) {
        perror("setsockopt SOE_BANDPING");
        exit(1);
    }
    return 0;
}